// ==========================================================================
// 云捷GO自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-08-01 08:34:56
// 生成路径: app/service/module/project/project_service.go
// 生成人：gfcms
// ==========================================================================
package project

import (
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	projectModel "myblog/app/model/module/project"
	"myblog/app/utils/convert"
	"myblog/app/utils/excel"
	"myblog/app/utils/page"
	"myblog/app/utils/upfile"
)

//根据主键查询数据
func SelectRecordById(id int64) (*projectModel.Entity, error) {
	return projectModel.FindOne("id", id)
}

//根据主键删除数据
func DeleteRecordById(id int64) bool {
	result, err := projectModel.Delete("id", id)
	if err == nil {
		affected, _ := result.RowsAffected()
		if affected > 0 {
			return true
		}
	}

	return false
}

//批量删除数据记录
func DeleteRecordByIds(ids string) int64 {
	idarr := convert.ToInt64Array(ids, ",")
	result, err := projectModel.Delete("id in (?)", idarr)
	if err != nil {
		return 0
	}

	nums, _ := result.RowsAffected()

	return nums
}

//添加数据
func AddSave(req *projectModel.AddReq, session *ghttp.Session) (int64, error) {
	var entity projectModel.Entity

	entity.Name = req.Name
	entity.Number = req.Number
	entity.Type = req.Type
	entity.Manager = req.Manager
	entity.Customer = req.Customer
	entity.Email = req.Email
	entity.Telephone = req.Telephone
	entity.Ordertime = req.Ordertime
	entity.Overtime = req.Overtime
	entity.CreateTime = gtime.Now()

	result, err := entity.Insert()
	if err != nil {
		return 0, err
	}

	id, err := result.LastInsertId()

	if err != nil || id <= 0 {
		return 0, err
	}
	return id, nil
}

//修改数据
func EditSave(req *projectModel.EditReq, session *ghttp.Session) (int64, error) {

	entity, err := projectModel.FindOne("id=?", req.Id)

	if err != nil {
		return 0, err
	}

	if entity == nil {
		return 0, gerror.New("数据不存在")
	}

	entity.Name = req.Name
	entity.Number = req.Number
	entity.Type = req.Type
	entity.Manager = req.Manager
	entity.Customer = req.Customer
	entity.Email = req.Email
	entity.Telephone = req.Telephone
	entity.Ordertime = req.Ordertime
	entity.Overtime = req.Overtime
	entity.UpdateTime = gtime.Now()

	result, err := entity.Save()

	if err != nil {
		return 0, err
	}

	rs, err := result.RowsAffected()

	if err != nil {
		return 0, err
	}

	return rs, nil
}

//根据条件查询数据
func SelectListAll(params *projectModel.SelectPageReq) ([]projectModel.Entity, error) {
	return projectModel.SelectListAll(params)
}

//根据条件分页查询数据
func SelectListByPage(params *projectModel.SelectPageReq) ([]projectModel.Entity, *page.Paging, error) {
	return projectModel.SelectListByPage(params)
}

// 导出excel
func Export(param *projectModel.SelectPageReq) (string, error) {
	result, err := projectModel.SelectListExport(param)
	if err != nil {
		return "", err
	}

	head := []string{"ID", "项目名称", "合同编号", "项目类型", "商务", "客户", "邮箱", "电话", "接单时间", "完结时间", "", ""}
	key := []string{"id", "name", "number", "type", "manager", "customer", "email", "telephone", "ordertime", "overtime", "create_time", "update_time"}
	url, err := excel.DownlaodExcel(head, key, result)

	if err != nil {
		return "", err
	}

	return url, nil
}

//下载模板
func ImportTemplate() (string, error) {
	head := []string{"项目名称", "合同编号", "项目类型", "商务", "客户", "邮箱", "电话", "接单时间", "完结时间"}
	url, err := excel.ImportTemplate(head)
	if err != nil {
		return "", err
	}
	return url, nil
}

//导入excel
func ImportData(files ghttp.UploadFiles) error {
	file, _ := upfile.Upfile(files)
	data, err := excel.ImportExcel(file[0])
	if err != nil {
		return err
	}
	var entity projectModel.Entity
	var projectdata []projectModel.Entity
	for _, v := range data[1:] {
		entity.Name = v[0]
		entity.Number = v[1]
		entity.Type = v[2]
		entity.Manager = v[3]
		entity.Customer = v[4]
		entity.Email = v[5]
		entity.Telephone = v[6]
		entity.Ordertime = gtime.New(v[7],"20060102")
		entity.Overtime = gtime.New(v[8],"20060102")
		projectdata = append(projectdata, entity)
	}
	_, err = projectModel.Insert(projectdata)
	if err != nil {
		return err
	}
	return nil
}
